<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="edit-form">
        <span class="page-tag">{{
          btnMsg == '更新信息' ? '信息详情页' : '添加员工页'
        }}</span>
        <el-divider></el-divider>
        <el-form
          ref="userForm"
          :model="userInfo"
          :rules="rules"
          label-width="150px"
        >
          <el-col :span="12">
            <!-- 姓名 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="姓名" prop="username">
                  <el-input
                    v-model="userInfo.username"
                    size="mini"
                    class="inputW"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 工号 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="工号" prop="workNumber">
                  <!-- 工号是系统生成的  禁用这个组件-->
                  <el-input
                    v-model="userInfo.workNumber"
                    disabled
                    size="mini"
                    class="inputW"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!--手机  -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="手机" prop="mobile">
                  <!-- disabled布尔值 取两次反转化为布尔值 -->
                  <el-input
                    v-model="userInfo.mobile"
                    :disabled="!!$route.params.id"
                    size="mini"
                    class="inputW"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="部门" prop="departmentId">
                  <!-- MARK 及联部门组件/类名样式会给到组件最外层template-->
                  <select-tree
                    v-model="userInfo.departmentId"
                    class="inputW"
                  ></select-tree>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="聘用形式" prop="formOfEmployment">
                  <el-select
                    v-model="userInfo.formOfEmployment"
                    size="mini"
                    class="inputW"
                  >
                    <el-option label="正式" :value="'1'" />
                    <el-option label="非正式" :value="'2'" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="入职时间" prop="timeOfEntry">
                  <el-date-picker
                    v-model="userInfo.timeOfEntry"
                    size="mini"
                    type="date"
                    value-format="yyyy-MM-dd"
                    class="inputW"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="转正时间" prop="correctionTime">
                  <el-date-picker
                    v-model="userInfo.correctionTime"
                    size="mini"
                    type="date"
                    class="inputW"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <!-- 员工照片 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="员工头像">
                  <!-- MARK 上传图片组件 -->
                  <image-upload v-model="userInfo.staffPhoto" />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 保存个人信息 -->
            <el-row type="flex">
              <el-col :span="12" style="margin-left: 150px">
                <el-button
                  v-perm="'emp-put'"
                  type="primary"
                  @click="saveData"
                  >{{ btnMsg }}</el-button
                >
                <el-button @click="$router.go(-1)">返回</el-button>
              </el-col>
            </el-row>
          </el-col>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import SelectTree from './components/select-tree.vue'
import ImageUpload from './components/image-upload.vue'
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
export default {
  components: { SelectTree, ImageUpload },
  computed: {
    btnMsg () {
      return this.$route.params.id ? "更新信息" : "添加信息"
    }

  },
  // mounted () {
  //   this.$nextTick(() => {
  //     this.$forceUpdate(); // 强制更新组件
  //   });
  // },
  created () {
    /**
     * // MARK route / router
     * route 代表的是当前的路由信息对象。它是Vue Router实例中的一部分
     * 包含了当前路径的信息，如路径名、路由参数、查询参数等。
     * route.params
     * 
     * router 是Vue Router的实例。
     * Vue Router是Vue.js的官方路由管理器，用于管理应用的路由。
     * router.push
     */
    // 获取路由参数的id 存在再获取
    // route实例中获取参数
    if (this.$route.params.id) { this.getEmployeeDetail() }
  },
  data () {
    return {
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }, {
          min: 1, max: 4, message: '姓名为1-4位'
        }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
          //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }, {
          validator: (rule, value, callback) => {
            if (this.userInfo.timeOfEntry) {
              if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                callback(new Error('转正时间不能小于入职时间'))
                return
              }
            }
            callback()
          }
        }]
      }

    }
  },
  methods: {
    saveData () {
      this.$refs.userForm.validate(async isOK => {
        if (isOK) {
          if (this.$route.params.id) {
            // 如果有参数 就是员工详情页
            await updateEmployee(this.userInfo)
            this.$message('更新员工信息成功')
          } else {
            // 没有参数就是新增员工
            await addEmployee(this.userInfo)
            this.$message.success('新增员工成功')
          }
          // 操作成功返回列表页
          this.$router.push('/employee')
        }
      })
    },
    // MARK 你好呀
    async getEmployeeDetail () {
      this.userInfo = await getEmployeeDetail(this.$route.params.id)
      // this.$nextTick(() => {
      //   this.$forceUpdate(); // 强制更新组件
      // });
    }
  }
}
</script>

<style scoped lang="scss">
.page-tag {
  font: 18px;
  padding: 5px 18px;
  // line-height: 1.5em;
  color: #1e8fff;
  border: 1px solid #188cff97;
  // border-radius: 3px;
  background: #ecf5ffa5;
}
.edit-form {
  margin: 28px;
  background: #fff;
  padding: 20px;
  // MARK 内部级联组件样式
  .inputW {
    width: 380px;
  }
}
</style>